import { doGet, appJsonPost, doDelete } from '@/network/request';

// 获取 CSRF Token
function getCsrfToken() {
    const name = 'XSRF-TOKEN=';
    const decodedCookie = decodeURIComponent(document.cookie);
    const cookies = decodedCookie.split(';');
    for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i].trim();
        if (cookie.indexOf(name) === 0) {
            return cookie.substring(name.length, cookie.length);
        }
    }
    console.warn('CSRF Token not found in cookies');
    return '';
}

/**
 * 通过id删除日志
 * @param id 日志ID
 * @returns 删除结果
 */
export function deleteByIdApi(id: string) {
    return doDelete({
        url: `/logs/${id}`,
        headers: {
            'X-XSRF-TOKEN': getCsrfToken() // 添加 CSRF Token
        }
    });
}

/**
 * 查询日志表格
 * @param searchData 查询条件
 * @returns 查询结果
 */
export function loadTableApi(searchData: object) {
    return appJsonPost({
        url: "/logs/queryPage",
        data: searchData,
        headers: {
            'X-XSRF-TOKEN': getCsrfToken() // 添加 CSRF Token
        }
    });
}
